package com.tanhua.server.controller;

/**
 * @Author: cl
 * @Date: 2022/5/6 - 05 - 06
 * @Description:
 */

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentService;
import com.tanhua.server.service.MovementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论点赞喜欢模块
 */
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private MovementService movementService;

    /**
     * 发布评论
     *
     * @param map
     * @return
     */
    @PostMapping()
    public ResponseEntity send(@RequestBody Map<String, String> map) {

        //动态id
        String movementId = map.get("movementId");
        //评论内容
        String comment = map.get("comment");
        commentService.send(movementId, comment);
        return ResponseEntity.ok("发表评论成功");
    }

    /**
     * 根据动态id和评论类型分页查询动态评论信息
     *
     * @param page
     * @param pageSize
     * @param movementId
     * @return
     */
    @GetMapping()
    public ResponseEntity findByMovementId(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize, String movementId) {

        PageResult pr = commentService.findByMovementId(page, pageSize, movementId);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论点赞
     *
     * @param commentId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String commentId) {

        Integer count = movementService.like(commentId);
        return ResponseEntity.ok(count);
    }

    /**
     * 评论取消点赞
     *
     * @param commentId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity disLike(@PathVariable("id") String commentId) {

        Integer count = movementService.disLike(commentId);
        return ResponseEntity.ok(count);
    }
}
